using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Linq;
using WPCoco;
using System.IO;

namespace WPCoco.Tilesets
{
    public static class TilesetCache
    {
        public static Tileset Load(string asset)
        {
            if (!key2tile.ContainsKey(asset))
            {
                Texture2D text = WPCoco.Coco.Load<Texture2D>(asset);
                XDocument doc = XDocument.Load("Content/" + asset + ".xml");
                Tileset tileset = Tileset.LoadFrom(doc, text);
                key2tile[asset] = tileset;
            }
            return key2tile[asset];
        }

        static Dictionary<string, Tileset> key2tile = new Dictionary<string, Tileset>();

        public static bool Unload(string asset, bool disposeTexture)
        {
            if (!key2tile.ContainsKey(asset)) return false;
            Tileset tileset = key2tile[asset];
            key2tile.Remove(asset);
            if (disposeTexture && !tileset.Texture.IsDisposed)
                tileset.Texture.Dispose();
            tileset = null;
            return true;
        }

        public static bool Unload(string asset) 
        {
            return Unload(asset, false);
        }

        public static int Count
        {
            get { return key2tile.Count; }
        }
    }
}
